package cn.wowjoy.pda.android_pharmacy.common.widget;

import android.app.Activity;
import android.content.DialogInterface;
import android.view.KeyEvent;
import android.view.View;

import java.util.List;

import cn.wowjoy.pda.android_pharmacy.R;
import cn.wowjoy.pda.android_pharmacy.ui.activity.hospital.pill.InHospitalPillProgressActivity;

import static cn.wowjoy.pda.android_pharmacy.R.style.dialog;


public class CreateDialog {
    static MDialog mAlertDialog;
    static MDialog waitDialog;

    static public MDialog waitingDialog(final Activity activity) {
        if (activity == null || activity.isFinishing()) {
            return null;
        }
        if (waitDialog == null)
            waitDialog = new MDialog(activity, R.style.dialog_no_bg);
//        waitDialog.setCanceledOnTouchOutside(false);
        waitDialog.setCancelable(false);
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (!waitDialog.isShowing() )
                    waitDialog.show();
            }
        });

        return waitDialog;
    }

    public static void dismiss(Activity activity) {
        if (activity != null && !activity.isFinishing() && waitDialog != null && waitDialog.isShowing()) {
            activity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    waitDialog.dismiss();
                    waitDialog = null;
                }
            });
        }
    }

    static public MDialog alertDialog(Activity activity, String hint, String bottonL, String bottonR, View.OnClickListener cancelBtnClickListener, View.OnClickListener confirmBtnClickListener) {
        if (activity == null || activity.isFinishing()) {
            return null;
        }

        MDialog alertDialog = new MDialog(activity, R.style.dialog_bg, hint, bottonL, bottonR, cancelBtnClickListener, confirmBtnClickListener);
        alertDialog.setCanceledOnTouchOutside(false);
        alertDialog.setCancelable(true);
        alertDialog.show();
        return alertDialog;
    }

    static public MDialog updateDialog(Activity activity, String title, String hint, String bottonL, String bottonR, View.OnClickListener cancelBtnClickListener, View.OnClickListener confirmBtnClickListener) {
        if (activity == null || activity.isFinishing()) {
            return null;
        }

        MDialog alertDialog = new MDialog(activity, R.style.dialog_no_bg, title, hint, bottonL, bottonR, cancelBtnClickListener, confirmBtnClickListener);
        alertDialog.setCanceledOnTouchOutside(false);
        alertDialog.setCancelable(true);
        alertDialog.show();
        return alertDialog;
    }

    static public MDialog infoDialog(Activity activity, String hint, int type) {
        if (activity == null || activity.isFinishing()) {
            return null;
        }

        MDialog alertDialog = new MDialog(activity, R.style.dialog_no_bg, hint, type);
        alertDialog.setCanceledOnTouchOutside(false);
        alertDialog.setCancelable(true);
        alertDialog.show();
        return alertDialog;
    }
    static public MDialog exitDialog(Activity activity, int check,View.OnClickListener mConfim ,String hint) {
        if (activity == null || activity.isFinishing()) {
            return null;
        }

        MDialog alertDialog = new MDialog(activity, dialog,check,mConfim,hint);
        alertDialog.setCanceledOnTouchOutside(false);
        alertDialog.setCancelable(true);
        alertDialog.show();
        return alertDialog;
    }
    static public MDialog success2Dialog(Activity activity, int check) {
        if (activity == null || activity.isFinishing()) {
            return null;
        }

        MDialog alertDialog = new MDialog(activity, dialog,check);
        alertDialog.setCanceledOnTouchOutside(false);
        alertDialog.setCancelable(true);
        alertDialog.show();
        return alertDialog;
    }
    static public MDialog successDialog(Activity activity,boolean ref) {
        if (activity == null || activity.isFinishing()) {
            return null;
        }

        MDialog alertDialog = new MDialog(activity, dialog,ref);
        alertDialog.setCanceledOnTouchOutside(false);
        alertDialog.setCancelable(true);
        alertDialog.show();
        return alertDialog;
    }
    static public MDialog successDialog(Activity activity,int check ,String title) {
        if (activity == null || activity.isFinishing()) {
            return null;
        }

        MDialog alertDialog = new MDialog(activity, dialog,check ,title);
        alertDialog.setCanceledOnTouchOutside(false);
        alertDialog.setCancelable(true);
        alertDialog.show();
        return alertDialog;
    }
    static public MDialog failDialog(Activity activity) {
        if (activity == null || activity.isFinishing()) {
            return null;
        }
        if(activity instanceof InHospitalPillProgressActivity){
            MDialog alertDialog = new MDialog(activity, dialog,2 ,"该药品包不存在，请重复复核");
            alertDialog.setCanceledOnTouchOutside(false);
            alertDialog.setCancelable(true);
            alertDialog.show();
            return alertDialog;
        }
        MDialog alertDialog = new MDialog(activity, dialog,2);
        alertDialog.setCanceledOnTouchOutside(false);
        alertDialog.setCancelable(true);
        alertDialog.show();
        return alertDialog;
    }
    //审核重复 弹框提示
    static public MDialog failDialog2(Activity activity) {
        if (activity == null || activity.isFinishing()) {
            return null;
        }

        MDialog alertDialog = new MDialog(activity, dialog,3);
        alertDialog.setCanceledOnTouchOutside(false);
        alertDialog.setCancelable(true);
        alertDialog.show();
        return alertDialog;
    }
    //上架过的药品
    static public MDialog failDialog3(Activity activity,String title ,String hint) {
        if (activity == null || activity.isFinishing()) {
            return null;
        }

        MDialog alertDialog = new MDialog(activity, dialog,4,title ,hint);
        alertDialog.setCanceledOnTouchOutside(false);
        alertDialog.setCancelable(true);
        alertDialog.show();
        return alertDialog;
    }
    //上架过的药品
    static public MDialog failSubmit(Activity activity, String title, String hint, View.OnClickListener mListener) {
        if (activity == null || activity.isFinishing()) {
            return null;
        }

        MDialog alertDialog = new MDialog(activity, dialog,7 ,title,hint ,mListener);
        alertDialog.setCanceledOnTouchOutside(false);
        alertDialog.setCancelable(true);
        alertDialog.show();
        return alertDialog;
    }

    static public MDialog listDialog(Activity activity, List<String> data) {
        if (activity == null || activity.isFinishing()) {
            return null;
        }

        MDialog alertDialog = new MDialog(activity, R.style.dialog_no_bg, data);
        alertDialog.setCanceledOnTouchOutside(true);
        alertDialog.setCancelable(true);
        alertDialog.show();
        return alertDialog;
    }

    static public MDialog mutiDialog(final Activity activity, int img, String msg, boolean anmi) {
        if (activity == null || activity.isFinishing()) {
            return null;
        }
        final MDialog mutiDialog = new MDialog(activity, R.style.dialog_no_bg, img, msg, anmi);
        mutiDialog.setCanceledOnTouchOutside(false);
        mutiDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                return true;
            }
        });
        if (!mutiDialog.isShowing())
            mutiDialog.show();
        return mutiDialog;
    }
}
